/**
 *	流程模块主Flow详情页使用
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-22
 */

package com.framework.Flow;




import com.framerock.baselib.RockList;
import com.framerock.db.DB;
import com.framerock.baselib.Rock;
import com.framework.ACore.Model;
import com.framework.Model.ModelDept;
import com.framework.Model.ModelFlowunit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class FlowXiang extends FlowBase {

    public FlowXiang(Flow flows) {
        super(flows);
    }

    //所有文件id
    public String allFileids = "";

    /**
     * 单据操作菜单
     * */
    public ArrayList<Map<String, String>> getOptmenu()
    {
        ArrayList<Map<String, String>> rows = new ArrayList<Map<String, String>>(),mlist;
        Map<String, String> da = Rock.getMap(),rs;
        String str;
        int i,to,type;
        if(mFlow.isCompanymode() && mRequest.COMPANYBOOL){
            mlist = Model.get(this.nDb,"flowmenu").getAll("`cid`="+mRequest.COMPANYID+" AND `modeid`="+mFlow.modeid+" AND `status`=1", "*", "`sort` ASC");
            if(mlist!=null)for(i=0; i<mlist.size(); i++){
                rs  = mlist.get(i);
                str = rs.get("where");
                if(!Rock.isEmpt(str))str = mFlow.getFlowwheres(str);
                if(!Rock.isEmpt(str)){
                    to = mFlow.mModel.getCount("`id`="+mFlow.mid+" AND `cid`="+mRequest.COMPANYID+" AND "+str+"");
                    if(to==0)continue;
                }
                str = rs.get("receid");
                if(!Rock.isEmpt(str) && !ModelDept.get(this.nDb).receidInaid(str, mRequest.USEAID))continue;

                da = Rock.getMap();
                da.put("name", rs.get("name"));
                da.put("color", rs.get("statuscolor"));
                da.put("statusname", rs.get("statusname"));
                da.put("lx", "99");
                da.put("type", rs.get("type"));
                da.put("menuid", rs.get("id"));
                da.put("uptype", rs.get("uptype"));
                da.put("issm", rs.get("issm"));
                da.put("isup", rs.get("isup"));
                da.put("num", rs.get("num"));
                type = Rock.getMapint(rs, "type");
                if(type==5)da.put("upcont", rs.get("upcont"));
                rows.add(da);
            }
        }

        if(mFlow.isEdit()) {
            da = Rock.getMap();
            da.put("name", "编辑");
            da.put("lx", "2");
            rows.add(da);

            da = Rock.getMap();
            da.put("name", "编辑(新窗口)");
            da.put("lx", "3");
            rows.add(da);
        }
        
        if(mFlow.isDelete()) {
	        da = Rock.getMap();
	        da.put("name", "删除");
	        da.put("lx", "4");
	        da.put("color", "#D9534F");
	        rows.add(da);
        }

        Boolean bo = mFlow.isRemind();
        String funitid = "";
        if(mFlow.isCompanymode()){
            funitid = ModelFlowunit.get(this.nDb).funitid("remind", mRequest.COMPANYID);
            if(Rock.isEmpt(funitid))bo = false;
        }
        if(bo) {
            String mess = Rock.getMapString(mFlow.moders, "summary");
            Map<String, String> info = mFlow.remindInfo();
            String name = "单据提醒设置";
            if(info != null)name = Rock.getMapString(info, "name", name);
            da = Rock.getMap();
            da.put("name", name);
            da.put("lx", "5");
            da.put("funitid", funitid);
            da.put("mess", Rock.replaceString(mess, mFlow.data));
            rows.add(da);
            if(info != null) {
                name = Rock.getMapString(info, "nameview");
                if(!Rock.isEmpt(name)){
                    da = Rock.getMap();
                    da.put("name", name);
                    da.put("lx", "6");
                    da.put("funitid", funitid);
                    rows.add(da);
                }
            }
        }

        return rows;
    }

    /**
     * 运行单据操作菜单
     * */
    public String optmenuRun(String menuid, String lx)
    {
        Map<String, String> rs = Model.get(this.nDb, "flowmenu").getOne("`id`="+menuid+" AND `modeid`="+mFlow.modeid+" AND `cid`="+mFlow.COMPANYID+"");
        if(rs==null)return mFlow.setError("单据操作菜单不存在");
        String sm       = mRequest.post("sm");
        String fileid   = mRequest.post("fileid");
        int islog       = Rock.getMapint(rs, "islog");

        Map<String, String> barr = mFlow.mFlowListener.onoptMenurunBefore(rs);
        if(barr != null){
            String msg = Rock.getMapString(barr, "msg");
            if(!Rock.isEmpt(msg))return mFlow.setError(msg);
        }

        String  upcont  = rs.get("upcont");
        if(!Rock.isEmpt(upcont)){
            upcont = mFlow.defaultValue(upcont);
            upcont = upcont.replace("{sm}", sm);
            Boolean bo = mFlow.mModel.update(upcont, "`id`="+mFlow.mid+"");
            if(!bo)return mFlow.setError(this.nDb.error());
        }

        if(islog==1){
            String name     = rs.get("name");
            String actname  = rs.get("actname");
            if(!Rock.isEmpt(actname))name = actname;
            mFlow.addLog(name, sm, new String[]{
                    "filecont", fileid,
                    "statusname", rs.get("statusname"),
                    "statuscolor", rs.get("statuscolor"),
            });
        }

        mFlow.mFlowListener.onoptMenurunAfter(rs);

        return "";
    }
    
    /**
     * 获取详情的数据
     * */
    public Map<String,String> getXiangdata()
    {
    	Map<String,String> data = this.mFlow.data,da,da1,da2;
    	data = mFlow.mFlowListener.onReplacers(data, 1);

    	int i,len,jlen,j,klen,k;
    	ArrayList<Map<String, String>> farrrep = new ArrayList<Map<String, String>>(),farr,store;
        String type,fid,val,nval,key,fileids = "";
        String varr[] = new String[]{"checkboxall","select","rockcombo","radio","checkbox"};

        farr    = mFlow.getFieldsArr(1);
        len     = farr.size();
        for(i=0; i<len;i++) {
            da      = farr.get(i);
            if(Rock.getMapint(da, "iszs")==0)continue;
            type    = da.get("fieldstype");
            fid     = da.get("fields");
            if(Rock.inArray(varr, type)){
                farrrep.add(da);
            }
            if(type.equals("uploadfile")){
                val = Rock.getMapString(mFlow.data, fid);
                if(!Rock.isEmpt(val)){
                    if(!Rock.isEmpt(fileids))fileids+=",";
                    fileids += val;
                }
            }
        }

        this.allFileids = fileids;

        jlen    = farrrep.size();
        Map<String, Map<String, String>> storeAll = new HashMap<String, Map<String, String>>();
        if(jlen>0){
            mFlow.mFlowBase.Store().getallStore(2); //加载数据源
            for(j=0;j<jlen;j++){
                fid     = farrrep.get(j).get("fields");
                store   = mFlow.mFlowBase.Store().getallStoreList(fid);
                if(store != null) {
                    klen = store.size();
                    da2  = Rock.getMap();
                    for(k=0;k<klen;k++){
                        da1 = store.get(k);
                        val = da1.get("value");
                        da2.put("val_"+val, da1.get("name"));
                        key = Rock.getMapString(da1, "color");
                        if(!Rock.isEmpt(key))da2.put("col_"+val, key);
                    }
                    storeAll.put(fid, da2);
                }
            }
        }

    	if(jlen>0)for(j=0;j<jlen;j++){
            fid = farrrep.get(j).get("fields");
            val = data.get(fid);
            if(Rock.isEmpt(val))continue;
            da1 = storeAll.get(fid);
            if(da1 != null) {
	            nval= replaceVal(val, da1, 0);
	            if(nval != null) {
                    data.put(fid+"_textcn", nval);
	            	nval = replaceVal(val, da1, 1);
	            	if(nval != null)data.put(fid+"_color", nval); //字体颜色
	            }
            }
        }


        if(mFlow.isflow > 0){
            RockList rocklist = mFlow.mFlowBase.Bill().getBilllist(mFlow.mid);
            da = rocklist.getKeyMap(mFlow.mid);
            if(da != null){
                data.put("nowcheckname", da.get("nowcheckname"));
                data.put("sericnum", da.get("sericnum"));
                String ztna = data.get("status_textcn");
                if(Rock.contain(ztna, "?"))data.put("status_textcn", ztna.replace("?", da.get("nowcheckname")));
            }
        }

    	//消息提醒标识已读
    	Model.get(this.nDb, "todo").update("`status`=1", "`cid`="+this.COMPANYID+" AND `mknum`='"+mFlow.modenum+"' AND `mid`="+mFlow.mid+" AND `status`=0");
    	return data;
    }
    
    //替换
    private String replaceVal(String key, Map<String, String> da, int lx)
    {
        if(da == null)return null;
        String nstr = "",kv,nv;
        if(lx==0){
            String arr[] = key.split(",");
            for(int i=0;i<arr.length;i++){
                kv = arr[i];
                nv = da.get("val_"+kv+"");
                if(nv==null)nv = kv;
                if(nstr.length()>0)nstr+=",";
                nstr+=nv;
            }
            return nstr;
        }
        if(lx==1){
            nstr = da.get("col_"+key+"");
        }
        return nstr;
    }

    /**
     * 删除
     * @param qxbo 是否需要权限判断
     * */
    public String delete(Boolean qxbo)
    {
        if(mFlow.isError())return "";
        if(qxbo && !mFlow.isDelete())return mFlow.setError("无删除权限");
        String type,where,str = mFlow.mFlowListener.onDeletebefore();
        if(!Rock.isEmpt(str))return mFlow.setError(str);

        mFlow.mModel.delete(mFlow.mid);
        Model mlog = Model.get(this.nDb, "flowlog");
        mlog.delete(mFlow.mwhere);
        String dwhere = "`mknum`='"+mFlow.modenum+"' AND `mid`="+mFlow.mid+"";
        Model.get(this.nDb, "todo").delete(dwhere);
        Model.get(this.nDb, "remind").delete(dwhere);

        ArrayList<Map<String, String>> farr = mFlow.getFieldsArr(0);
        Map<String, String> da,rs;
        for(int i=0; i<farr.size();i++){
            da      = farr.get(i);
            type    = da.get("fieldstype");
            //删子表
            if(type.equals("subdata")) {
                rs  = mFlow.subdataInfo(da.get("data"));
                if(rs==null)continue;
                Flow flow = mFlow.let(rs.get("num"));
                where= "`"+rs.get("fid")+"`="+mFlow.mid+"";
                str = DB.dbSql(DB.chuliPerfix(flow.table), "id", where);
                mlog.delete("`table`='"+flow.table+"' AND `mid` IN("+str+")");
                flow.mModel.delete(where);
            }
            //删上传文件
        }
        mFlow.mFlowListener.onDeleteafter();
        mFlow.sendTodo(FlowTodo.ACTTYPE_DEL);
        return "";
    }

    public String getAllFileids()
    {
        return this.allFileids;
    }
}
